<template>
  <div id="add-blog">
    <h2>添加博客</h2>
    <form v-if="!submited">
      <label>博客标题</label>
      <input type="text" v-model="blog.title" required/>
      <label>博客内容</label>
      <textarea type="text" v-model="blog.content" required/>
      <div id="checkboxes">
        <label>Vue.js</label>
        <input v-model="blog.categories" type="checkbox" value="Vue.js"/>
        <label>Node.js</label>
        <input v-model="blog.categories" type="checkbox" value="Node.js"/>
        <label>React.js</label>
        <input v-model="blog.categories" type="checkbox" value="React.js"/>
        <label>Angular4.js</label>
        <input v-model="blog.categories" type="checkbox" value="Angular4.js"/>
      </div>
      <label>作者：</label>
      <select v-model="blog.author">
        <option v-for="author in authors">
          {{author}}
        </option>
      </select>
      <!--prevent阻止刷新页面-->
      <button v-on:click.prevent="post">添加博客</button>
    </form>
    <hr/>
    <div v-if="submited">
      <h3>您的博客添加成功!</h3>
    </div>
    <div id="previw">
      <h3>博客总览</h3>
      <p>博客标题：{{blog.title}}</p>
      <p>博客内容</p>
      <p>{{blog.content}}</p>
      <p>博客分类：</p>
      <ul>
        <li v-for="category in blog.categories">
          {{category}}
        </li>
      </ul>
      <p>作者：{{blog.author}}</p>
    </div>
  </div>
</template>

<script>
  export default {
    name: "add-blog",
    data() {
      return {
        blog: {
          title: "",
          content: "",
          categories: [],
          author: ""
        },
        authors: ["hemiah", "Henry", "buk"],
        submited: false
      }
    },
    methods: {
      post: function () {
        this.$http.post("http://jsonplaceholder.typicode.com/posts", {
          title: this.blog.title,
          body: this.blog.content,
          userId: 1
        }).then(function (data) {
          console.log(data)
          this.submited = true
        })
      }
    }
  }
</script>

<style scoped>
  #add-blog * {
    box-sizing: border-box;
  }

  #add-blog {
    margin: 20px auto;
    max-width: 560px;
    padding: 20px;
    background: #6677cc;
  }

  label {
    display: block;
    margin: 20px 0 10px;
  }

  textarea {
    height: 200px;
  }

  input[type="text"], textarea, select {
    display: block;
    width: 100%;
    padding: 8px;
  }

  #checkboxes label {
    display: inline-block;
    margin-top: 0;
  }

  #checkboxes input {
    display: inline-block;
    margin-top: 10px;
  }

  button {
    display: block;
    margin: 20px 0;
    background: crimson;
    color: #fff;
    border: 0;
    padding: 10px;
    border-radius: 4px;
    font-size: 18px;
    cursor: pointer;
  }

  #previw {
    padding: 10px 20px;
    border: 1px dotted #ccc;
    margin: 30px 0;
    background: #eee;
  }

  h3 {
    margin-top: 10px;
  }
</style>
